"""
Created on 12-Aug-2009

@author: dexen
"""

DEAD = -1
ALIVE = -2

from dexen.system.base import score

class Individual(object):
    
    def __init__(self):
        self.life_state = ALIVE
        self.__scores__ = {}
        self.__files__ = {}
        self.__birth_id__ = None
        self.__death_id__ = None
        self.__strip_exclusion__ = ['__strip_exclusion__', '__id__', 'life_state', '__scores__', '__modified__', '__birth_id__', "__death_id__"]
        
    def kill(self):
        self.life_state = DEAD
    
    def revive(self):
        self.life_state = ALIVE
    
    def __isalive__(self):
        return self.life_state == ALIVE
        
    def __setattr__(self, name, value):
        if name == "__modified__":
            object.__setattr__(self, name, value)
            return
        if not hasattr(self, "__modified__"):
            self.__modified__ = {}
        self.__modified__[name] = value
        object.__setattr__(self, name, value)
    
    def _isnew(self):
        return not hasattr(self, "__id__")

    def set_score(self, name, label, type):
        self.__scores__[name] = score.Score(self, name, label, type)
        self.__strip_exclusion__.append(name)

    """
    def get_score(self, name):
        if not self.__scores__.has_key(name):
            return None
        return self.__scores__[name]
    """
    
    def archive_data(self, name, content):
        self.__files__[name] = str(content)
    
    def strip_archived(self):
        self.__files__ = {}

    def get_archived_data(self):
        return self.__files__

    def _for_strip(self, name):        
        if name in self.__strip_exclusion__:
            return False
        #if name in self.__scores__.keys():
        #    return False
        return True
        
    def strip_all(self):
        for name in self.__dict__.keys():
            if self._for_strip(name):
                setattr(self, name, None)
            
    def dominates(self, ind):
        scores2 = ind.__scores__

        better_flg = False
        for name, score_obj in self.__scores__.items():

            score2_obj = scores2[name]
            if score2_obj.is_better(score_obj):
                return False
            if score_obj.is_better(score2_obj):
                better_flg = True
        
        return better_flg

#===============================================================================
# TESTING
#===============================================================================
import cPickle

class A(object):
    def __init__(self):
        self.k = 5

def test():
    ind = Individual()
    ind.a = "haha"
    ind.b = [1,2,4,5]
    ind.c = A()
    print ind.__modified__.keys()
    ind.__modified__ = {}
    raw = cPickle.dumps(ind)
    ind = cPickle.loads(raw)
    print ind.__modified__.keys()
    
def test2():
    ind = Individual()
    ind.haha = 4
    ind.set_score("haha", "da", score.MINIMIZE)
    ind.veli = 5
    ind.__id__ = 3
    ind.archive_data("ali.txt", "Korkma sonmez bu safa")
    ind.archive_data("veli ka.txt", range(20))
    print ind.get_archived_data()
    ind.strip_archived()
    print ind.get_archived_data()
    ind.strip_all()
    print ind.__dict__
    
def main():
    #test()    
    test2()
    
if __name__ == "__main__":
    main()
